﻿@model SearchCriteriaVM

@{
    ViewBag.Title = "ผลการค้นหา";
    int i = 1;
    int page = 1;
    SearchResult searchResult = ViewBag.SearchResult;
    double pageCount = 0;
    bool isSearchTitleOnly = searchResult != null && searchResult.SearchTitleOnly;
    bool isSearchExactName = searchResult != null && searchResult.SearchByEmployeeNameExactName;
    string searchOption = searchResult != null ? searchResult.SeacrhOption : SeacrhOptionEnum.ทั้งหมด;
    string sortResultByTypeEnum = searchResult != null ? searchResult.SortResultByType : SortResultByTypeEnum.วันที่;
    string sortResultOrdering = searchResult != null ? searchResult.SortResultOrdering : SortResultOrderingEnum.มากไปน้อย;
    string searchByEmployeeName = searchResult != null ? searchResult.SearchByEmployeeName : "";
}

@if(!string.IsNullOrWhiteSpace(ViewBag.ErrorMessage))
{
    <div class="alert alert-block alert-error">
        @ViewBag.ErrorMessage
    </div>
}
@section pageSpecificPluginStyles
{
    <link href="~/Content/Ace/assets/css/jPaginate.css" rel="stylesheet" />
    <style>
        .content {
            margin-top: 100px;
            padding: 0px;
            bottom: 0px;
        }
        .demo {
            border: 1px solid #fff;
            padding: 50px 10px 10px 10px; 
        }
        .pagedemo {
            font-size: small;
            border: 1px solid #CCC;
            padding: 10px 10px 20px 10px;
            background-color: white;
        }
        a:hover {
            text-decoration: none;
        }
        .SearchResultNavPath {
            color: gray;
        }
        .ResultSiteNav {
            margin: 0 0 10px 0; 
        }
        .resultLink {
            line-height: 80%;
        }
        .pagemessage {
            font-size: 14px;
            margin: 10px 0 3px 0;
        }
        label {
            display: block;
        }
        .tdCaption {
            vertical-align: text-top;
            font-weight: bold;
            font-size: 14px;
            width:140px;
            text-align: right;
        }
         .tdInput {
            vertical-align: text-top;
         }
         .numbering {
             color:gray
         }
    </style>
}
@if(ViewBag.Message != null && ViewBag.Message != "")
{
    <div class="alert alert-box danger">@ViewBag.Message
        <a  class="close" role="button" href="@Url.Action("Search", "Search")">
            <i class="icon-remove"></i>
        </a>
    </div>
}


@using (Html.BeginForm("SearchKeywords", "Search", FormMethod.Post, new { @class = "form-search" }))
{
    <div class="span12">
		<div class="tabbable tabs-below">
			<div class="tab-content">
				<div id="searchPanel" class="tab-pane active">
					
                    <div class="row-fluid">
                        <div class="span1">
                        </div> 
                        <div class="input-icon span6">
                            <input type="text" placeholder="ค้นหาด้วยคำค้น ..." 
                                    class="search-query input-large " 
                                    id="keywords2" name="keywords2"
                                    autocomplete="on" 
                                    style="width:100%" 
                                    value ='@((searchResult != null && !string.IsNullOrWhiteSpace(searchResult.SearchKeyword) ?
                                searchResult.SearchKeyword : "").Trim())'/>
                            <i class="icon-search" id="nav-search-icon"></i>
                        </div>
                        <div class="span3">
                            <input class=" btn btn-small btn-info" type="submit" value="ค้น" />
                        </div>
                    </div>
				</div>

                <div id="configPanel" class="tab-pane">
                    
                    @Html.Hidden("RequestByEmpId", GimsMembership.CurrentEmpId) 
                    
                    <table style="width:100%">
                        <tr>
                            <td class="tdCaption">
                                การค้นหา
                            </td>
                            <td style="width:35%" class="tdInput">
                                <div class="controls">
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SearchTitleOnly" type="radio" value="1"  @(isSearchTitleOnly ? "checked='checked'" : "")>
                                        <span class="lbl"> ค้นหัวข้อเท่านั้น</span>
                                    </label>
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SearchTitleOnly" type="radio" value="0" @(!isSearchTitleOnly ? "checked='checked'" : "")>
                                        <span class="lbl"> ค้นรายละเอียดและหัวข้อ</span>
                                    </label> 

                                </div>
                            </td>
                            
                            <td  class="tdCaption">
                                ข้อมูลที่ค้นหา
                            </td>
                            <td style="width:35%" class="tdInput">
                                <div class="controls">
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SeachOption" type="radio" value="@SeacrhOptionEnum.ทั้งหมด"  
                                            @(searchOption == SeacrhOptionEnum.ทั้งหมด ? "checked='checked'" : "")>
                                        <span class="lbl">@SeacrhOptionEnum.ทั้งหมด</span>
                                    </label>
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SeachOption" type="radio" value="@SeacrhOptionEnum.จำหน่ายพัสดุ" 
                                            @(searchOption == SeacrhOptionEnum.จำหน่ายพัสดุ ? "checked='checked'" : "")>
                                        <span class="lbl">@SeacrhOptionEnum.จำหน่ายพัสดุ</span>
                                    </label>  
                                    
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SeachOption" type="radio" value="@SeacrhOptionEnum.ประเภทพัสดุ" 
                                            @(searchOption == SeacrhOptionEnum.ประเภทพัสดุ ? "checked='checked'" : "")>
                                        <span class="lbl">@SeacrhOptionEnum.ประเภทพัสดุ</span>
                                    </label> 
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SeachOption" type="radio" value="@SeacrhOptionEnum.หมวดพัสดุ" 
                                            @(searchOption == SeacrhOptionEnum.หมวดพัสดุ ? "checked='checked'" : "")>
                                        <span class="lbl">@SeacrhOptionEnum.หมวดพัสดุ</span>
                                    </label> 
                                    
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SeachOption" type="radio" value="@SeacrhOptionEnum.กลุ่มพัสดุ" 
                                            @(searchOption == SeacrhOptionEnum.กลุ่มพัสดุ ? "checked='checked'" : "")>
                                        <span class="lbl">@SeacrhOptionEnum.กลุ่มพัสดุ</span>
                                    </label> 
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SeachOption" type="radio" value="@SeacrhOptionEnum.รายการพัสดุ" 
                                            @(searchOption == SeacrhOptionEnum.รายการพัสดุ ? "checked='checked'" : "")>
                                        <span class="lbl">@SeacrhOptionEnum.รายการพัสดุ</span>
                                    </label> 
                                    
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SeachOption" type="radio" value="@SeacrhOptionEnum.แจ้งความประสงค์" 
                                            @(searchOption == SeacrhOptionEnum.แจ้งความประสงค์ ? "checked='checked'" : "")>
                                        <span class="lbl">@SeacrhOptionEnum.แจ้งความประสงค์</span>
                                    </label> 
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SeachOption" type="radio" value="@SeacrhOptionEnum.รายการขอซื้อจ้าง" 
                                            @(searchOption == SeacrhOptionEnum.รายการขอซื้อจ้าง ? "checked='checked'" : "")>
                                        <span class="lbl">@SeacrhOptionEnum.รายการขอซื้อจ้าง</span>
                                    </label> 
                                    
                                    
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SeachOption" type="radio" value="@SeacrhOptionEnum.ใบสั่งงานสัญญา" 
                                            @(searchOption == SeacrhOptionEnum.ใบสั่งงานสัญญา ? "checked='checked'" : "")>
                                        <span class="lbl">@SeacrhOptionEnum.ใบสั่งงานสัญญา</span>
                                    </label> 
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SeachOption" type="radio" value="@SeacrhOptionEnum.รายการตรวจรับ" 
                                            @(searchOption == SeacrhOptionEnum.รายการตรวจรับ ? "checked='checked'" : "")>
                                        <span class="lbl">@SeacrhOptionEnum.รายการตรวจรับ</span>
                                    </label> 
                                    
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SeachOption" type="radio" value="@SeacrhOptionEnum.ค่าเสื่อม" 
                                            @(searchOption == SeacrhOptionEnum.ค่าเสื่อม ? "checked='checked'" : "")>
                                        <span class="lbl">@SeacrhOptionEnum.ค่าเสื่อม</span>
                                    </label> 
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SeachOption" type="radio" value="@SeacrhOptionEnum.ระเบียบจัดซื้อจ้าง" 
                                            @(searchOption == SeacrhOptionEnum.ระเบียบจัดซื้อจ้าง ? "checked='checked'" : "")>
                                        <span class="lbl">@SeacrhOptionEnum.ระเบียบจัดซื้อจ้าง</span>
                                    </label> 
                                    
                                    
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SeachOption" type="radio" value="@SeacrhOptionEnum.หน่วยนับ" 
                                            @(searchOption == SeacrhOptionEnum.หน่วยนับ ? "checked='checked'" : "")>
                                        <span class="lbl">@SeacrhOptionEnum.หน่วยนับ</span>
                                    </label> 
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SeachOption" type="radio" value="@SeacrhOptionEnum.ทะเบียนผู้ขาย" 
                                            @(searchOption == SeacrhOptionEnum.ทะเบียนผู้ขาย ? "checked='checked'" : "")>
                                        <span class="lbl">@SeacrhOptionEnum.ทะเบียนผู้ขาย</span>
                                    </label> 
                                    
                                    
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SeachOption" type="radio" value="@SeacrhOptionEnum.คลังพัสดุ" 
                                            @(searchOption == SeacrhOptionEnum.คลังพัสดุ ? "checked='checked'" : "")>
                                        <span class="lbl">@SeacrhOptionEnum.คลังพัสดุ</span>
                                    </label> 
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SeachOption" type="radio" value="@SeacrhOptionEnum.เบิก" 
                                            @(searchOption == SeacrhOptionEnum.เบิก ? "checked='checked'" : "")>
                                        <span class="lbl">@SeacrhOptionEnum.เบิก</span>
                                    </label> 
                                    
                                    
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SeachOption" type="radio" value="@SeacrhOptionEnum.โอน" 
                                            @(searchOption == SeacrhOptionEnum.โอน ? "checked='checked'" : "")>
                                        <span class="lbl">@SeacrhOptionEnum.โอน</span>
                                    </label> 
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SeachOption" type="radio" value="@SeacrhOptionEnum.จ่าย" 
                                            @(searchOption == SeacrhOptionEnum.จ่าย ? "checked='checked'" : "")>
                                        <span class="lbl">@SeacrhOptionEnum.จ่าย</span>
                                    </label> 
                                    
                                    
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SeachOption" type="radio" value="@SeacrhOptionEnum.ตรวจสอบ" 
                                            @(searchOption == SeacrhOptionEnum.ตรวจสอบ ? "checked='checked'" : "")>
                                        <span class="lbl">@SeacrhOptionEnum.ตรวจสอบ</span>
                                    </label> 
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SeachOption" type="radio" value="@SeacrhOptionEnum.ทะเบียนคุมพัสดุ" 
                                            @(searchOption == SeacrhOptionEnum.ทะเบียนคุมพัสดุ ? "checked='checked'" : "")>
                                        <span class="lbl">@SeacrhOptionEnum.ทะเบียนคุมพัสดุ</span>
                                    </label> 
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SeachOption" type="radio" value="@SeacrhOptionEnum.คำขออนุมัติ" 
                                            @(searchOption == SeacrhOptionEnum.คำขออนุมัติ ? "checked='checked'" : "")>
                                        <span class="lbl">@SeacrhOptionEnum.คำขออนุมัติ</span>
                                    </label> 
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SeachOption" type="radio" value="@SeacrhOptionEnum.การลา" 
                                            @(searchOption == SeacrhOptionEnum.การลา ? "checked='checked'" : "")>
                                        <span class="lbl">@SeacrhOptionEnum.การลา</span>
                                    </label> 
                                    
                                    
                                </div>
                            </td>
                            <td rowspan="2"><input class=" btn btn-small btn-info" type="submit" value="ค้น" /></td>
                        </tr>
                        
                        <tr>
                            <td  class="tdCaption">
                                จัดเรียงข้อมูลโดย
                            </td>
                            <td class="tdInput">
                                <div class="controls">
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SortResultByType" type="radio" value="@SortResultByTypeEnum.กระทู้" 
                                            @(sortResultByTypeEnum == SortResultByTypeEnum.กระทู้ ? "checked='checked'" : "")>
                                        <span class="lbl">@SortResultByTypeEnum.กระทู้</span>
                                    </label>
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SortResultByType" type="radio" value="@SortResultByTypeEnum.วันที่"
                                            @(sortResultByTypeEnum == SortResultByTypeEnum.วันที่ ? "checked='checked'" : "")>
                                        <span class="lbl"> @SortResultByTypeEnum.วันที่</span>
                                    </label> 
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SortResultByType" type="radio" value="@SortResultByTypeEnum.ชื่อผู้ใช้งาน"
                                            @(sortResultByTypeEnum == SortResultByTypeEnum.ชื่อผู้ใช้งาน ? "checked='checked'" : "")>
                                        <span class="lbl"> @SortResultByTypeEnum.ชื่อผู้ใช้งาน</span>
                                    </label> 

                                </div>
                            </td>
                            
                            <td  class="tdCaption">
                                ตามลำดับ
                            </td>
                            <td class="tdInput">
                                <div class="controls">
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SortResultOrdering" type="radio" value="@SortResultOrderingEnum.มากไปน้อย"  
                                            @(sortResultOrdering == SortResultOrderingEnum.มากไปน้อย ? "checked='checked'" : "")>
                                        <span class="lbl">@SortResultOrderingEnum.มากไปน้อย</span>
                                    </label>
                                    &nbsp;&nbsp;
                                    <label>
                                        <input name="SortResultOrdering" type="radio" value="@SortResultOrderingEnum.น้อยไปมาก"  
                                            @(sortResultOrdering == SortResultOrderingEnum.น้อยไปมาก ? "checked='checked'" : "")>
                                        <span class="lbl">@SortResultOrderingEnum.น้อยไปมาก</span>
                                    </label> 

                                </div>
                            </td>
                        </tr>
                        
                        <tr>
                            <td  class="tdCaption">
                                ชื่อผู้แก้ไขข้อมูล
                            </td>
                            <td class="tdInput"> 
                                <input type="text" name="searchByEmployeeName" id="searchByEmployeeName" value="@searchByEmployeeName"/>
                                <label>
                                    <input name="SearchByEmployeeNameExactName" id="SearchByEmployeeNameExactName" type="checkbox" value="on" @(isSearchExactName ? "checked=checked" : "")>
                                    <span class="lbl"> ตรงกันทั้งชื่อ</span>
								</label>
                            </td>
                            
                            <td  class="tdCaption">

                            </td>
                            <td class="tdInput">
                                
                            </td>
                        </tr>

                    </table>
                    
                     
                </div>
			</div>

			<ul class="nav nav-tabs" id="myTab2">
				<li class="active">
					<a data-toggle="tab" href="#searchPanel">ค้นหา</a>
				</li>

                <li class="">
                    <a data-toggle="tab" href="#configPanel">ตั้งค่า</a>
                </li>

			</ul>
		</div>
	</div>
}

@if(searchResult != null)
{
    pageCount = Math.Ceiling(searchResult.ResultAmount/10d);
    
    <div id="paginationdemo" class="demo">
        <h2 class="green">ผลการค้นด้วยคำค้น "<strong class="purple">@searchResult.SearchKeyword</strong>" จำนวน @searchResult.ResultAmount ผลลัพธ์</h2>
    
        <div id="pageListbottom">                   
        </div>
        <div class="pagemessage">หน้า <span id="pageNo">1</span> จาก @pageCount หน้า</div>
        @if (searchResult.ResultAmount > 1)
        {
@:@Html.Raw("<div id=\"p1\" class=\"pagedemo " + (i == 1 ? "_current" : "") + "\" style=\"\">\r\n")
        }

        @foreach (var data in searchResult.SearchResultDtls)
        {
            if (i%10 == 0)
            {
                page++;
                @Html.Raw("</div>\r\n\r\n")
                @Html.Raw("<div id=\"p" + page + "\" class=\"pagedemo " + (i == 1 ? "_current" : "") + "\" style=\"display:none;\">\r\n\r\n")
            }
            var dataDateTime = data.DataUpdatedWhen ?? data.DataCreatedWhen;
            var dataText = Html.Encode(data.DataName ?? "");
            var dataDesc = Html.Encode(data.AllText ?? "");
            dataText = dataText.Substring(0, Math.Min(dataText.Length, 300)) + (dataText.Length > 300 ? "..." : string.Empty);
            string dataUrl;
            switch(data.DataSeacrhOptionEnum)
            { 
                case "ประเภทพัสดุ": dataUrl = Url.Action("Details", "MasResourceType", new{id = data.DataId});break;
                case "หมวดพัสดุ":dataUrl = Url.Action("Details", "MasResourceGroup", new{id = data.DataId});break;
                case "กลุ่มพัสดุ":dataUrl = Url.Action("Details", "MasResourceSubGroup", new{id = data.DataId});break;
                case "รายการพัสดุ":dataUrl = Url.Action("Details", "MasResourceItem", new{id = data.DataId});break;
                case "แจ้งความประสงค์":dataUrl = Url.Action("Details", "PrePurchase", new{id = data.DataId});break;
                case "รายการขอซื้อจ้าง":dataUrl = Url.Action("Details", "Purchase", new{id = data.DataId});break;
                case "ใบสั่งงานสัญญา":dataUrl = Url.Action("Details", "PurchaseOrder", new{id = data.DataId});break;
                case "รายการตรวจรับ":dataUrl = Url.Action("Details", "DeliveryNote", new{dnid = data.DataId});break; 
                case "ระเบียบจัดซื้อจ้าง":dataUrl = Url.Action("Details", "MasPurchaseRegular", new{id = data.DataId});break;
                case "หน่วยนับ":dataUrl = Url.Action("Details", "MasUnit", new{id = data.DataId});break;
                case "ทะเบียนผู้ขาย":dataUrl = Url.Action("Details", "MasContact", new{id = data.DataId});break;
                case "คลังพัสดุ":dataUrl = Url.Action("Details", "Warehouse", new{id = data.DataId});break;
                case "เบิก":dataUrl = Url.Action("Details", "RequestParcel", new{id = data.DataId});break;
                case "โอน":dataUrl = Url.Action("Details", "Movement", new{id = data.DataId});break;
                case "จ่าย":dataUrl = Url.Action("Details", "Dispense", new{id = data.DataId});break;
                case "ตรวจสอบ":dataUrl = Url.Action("Details", "xxx", new{id = data.DataId});break;
                case "ทะเบียนคุมพัสดุ":dataUrl = Url.Action("Details", "ParcelControlAssetList", new{id = data.DataId});break;
                case "จำหน่ายพัสดุ":dataUrl = Url.Action("Details", "xxx", new{id = data.DataId});break;
                case "ค่าเสื่อม":dataUrl = Url.Action("Details", "MasDepreciationType", new{id = data.DataId});break;
                case "การลา":dataUrl = Url.Action("Details", "AbsenceAdmin", new{id = data.DataId});break;
                case "คำขออนุมัติ":dataUrl = Url.Action("Details", "Task", new{id = data.DataId});break;
                default:dataUrl = "";break;
            }
            var moreLink = string.Format("<a href='{0}'>มีต่อ</a>", dataUrl);
            dataDesc = dataDesc.Substring(0, Math.Min(dataDesc.Length, 600)) + (dataDesc.Length > 600 ? "..." + moreLink + "..." : string.Empty);
            var nameLastModifier1 = string.IsNullOrWhiteSpace(data.DataUpdatedByFullName) ? data.DataCreatedByFullName : data.DataUpdatedByFullName;
            var nameLastModifier = nameLastModifier1;//string.IsNullOrWhiteSpace(nameLastModifier1) ? "(ไม่ทราบชื่อผู้แก้ไขข้อมูลล่าสุด" + data.DataCreatedBy + ")" : nameLastModifier1;
            foreach(var keyword in (searchResult.SearchKeyword??"").Split(' '))
            { 
                if(string.IsNullOrWhiteSpace(keyword))
                {
                    continue;
                }
                
                dataText = dataText.Replace(keyword, "<span class='lighter red'>" + keyword + "</span>");
                dataDesc = dataDesc.Replace(keyword, "<span class='lighter red'>" + keyword + "</span>");
            }
            <a href="@dataUrl" class="resultLink">
                <h5 style="line-height:20%" title="@dataUrl"><span class="numbering">#@i </span> &nbsp; @data.DataCode - @Html.Raw(dataText)</h5>
                <span class="green">@dataUrl</span><span class="ResultSiteNav">@Html.Raw(data.DataSiteNavigatePath)</span> <span style="color:#6fb3e0">@nameLastModifier1</span>
            </a>
            <p style="margin-bottom:30px; " title="@nameLastModifier">
                <span style="color:#ff61c0">@data.DataSeacrhOptionEnum</span> <span style="color:grey">@dataDateTime</span> @Html.Raw(dataDesc)
            </p>
            i++;
        }

        @if (i - 1%10 != 0)
        {
            @Html.Raw("     </div>\r\n\r\n")
        }
    </div>
}  

@section scripts{
    <script type="text/javascript">

        $("#pageListbottom").paginate({
            count: '@pageCount',
            start: 1,
            display: 20,
            border: true,
            border_color: '#fff',
            text_color: '#fff',
            background_color: '#6fb3e0',
            border_hover_color: '#ccc',
            text_hover_color: '#000',
            background_hover_color: '#fff',
            images: false,
            mouse: 'press',
            onChange: function (page) {
                $('._current', '#paginationdemo').removeClass('_current').hide();
                $('#p' + page).addClass('_current').show(); 
                $('#pageNo').text(page);
            }
        });
    </script>
}